// coupon.js
const app = getApp()
import { requestApi } from '../../../api'
import { formatPrice } from '../../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 页面配置
    loading: false,
    channel: '',
    winWidth: 0,
    winHeight: 0,
    flag: false,
    currentTab: 1,        // 当前页面  1未使用  2已使用  3已过期
    page: 1,
    total: {},
    couponList: [],
    requestFlag: false,
    couponData: '',
    useflag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let flag = options.flag || false
    let sellerUid = options.seller_uid || ''
    let gid = options.gid || ''
    let skuId = options.sku_id || ''
    let channel = options.channel || 'wx_xcx'

    /**
     * 获取系统信息
     */
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          flag: flag,
          sellerUid: sellerUid,
          gid: gid,
          skuId: skuId,
          channel: channel,
        });
      }
    });

    if (flag) {// 获取可用优惠券
      let data = {
        page: 1,
        sellerUid: sellerUid,
        gid: gid,
        skuId: skuId,
        channel: channel,
      }
      this.getUseCouponsList(data)
    } else {// 全部优惠券
      this.getCouponList(this.data.currentTab, 1, channel)
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  scrolltolower: function () {
      let status = this.data.currentTab
      let page = this.data.page + 1
      let flag = this.data.flag
      let total
      this.setData({
        loading: true
      })
    if (flag) {//可用优惠券
      let data = {
        page: page,
        sellerUid: this.data.sellerUid,
        gid: this.data.gid,
        skuId: this.data.skuId,
        channel: this.data.channel
      }
      this.getUseCouponsList(data)

    } else {// 全部优惠券
      this.getCouponList(status, page, this.data.channel)
    }

    this.setData({
      page: page
    })
  },

  // 获取可用优惠券列表
  getUseCouponsList: function (options) {
    if (this.data.requestFlag) return false

    this.setData({
      requestFlag: true,
    })
    let couponList = this.data.couponList
    let page = options.page
    let count = 20;
    let start = (page - 1) * count;
    app.request({
      url: requestApi.useCouponsListUrl,
      method: 'GET',
      data: {
        start: start,
        count: count,
        seller_uid: options.sellerUid,
        gid: options.gid,
        sku_id: options.skuId,
        channel: options.channel
      }
    }, (res) => {
      if (res.original.data.list) {
        for (let i = 0, len = res.original.data.list.length; i < len; i++) {
          res.original.data.list[i].amount = res.original.data.list[i].amount ? formatPrice(res.original.data.list[i].amount / 100) : 0
        }

        couponList = couponList.concat(res.original.data.list)
      }

      this.setData({
        loading: true,
        couponList: couponList,
        page: options.page
      })
    }, (error) => {
      this.setData({
        loading: true,
      })
      wx.showModal({
        title: '提示',
        content: error,
        showCancel: false
      })
      
    }, () => {
      this.setData({
        requestFlag: false,
      })
    })
  },

  // 获取所有优惠券列表数据
  getCouponList: function (status, page, channel) {
    if (this.data.requestFlag) return false

    this.setData({
      requestFlag: true,
    })
    
    let couponList = this.data.couponList
    let sellerUid = this.data.sellerUid

    let count = 20;
    let start = (page - 1) * count;

    app.request({
      url: requestApi.couponListUrl,
      method: 'GET',
      data: {
        status: status,
        start: start,
        count: count,
        seller_uid: sellerUid,
        channel: channel,
      }
    }, (data) => {
      let use_range, use_condition, forward_type, seller_uid

      if (data.original.data) {
        for (let i = 0, len = data.original.data.length; i < len; i++) {
          data.original.data[i].amount = data.original.data[i].amount ? formatPrice(data.original.data[i].amount / 100) : 0
          
        }

        couponList = couponList.concat(data.original.data)
      }

      this.setData({
        loading: true,
        total: data.original.total,
        couponList: couponList,
        page: page,
      })
    }, (error) => {
      this.setData({
        loading: true,
      })
      wx.showModal({
        title: '提示',
        content: error,
        showCancel: false
      })
    }, () => {
      this.setData({
        requestFlag: false,
      })
    })
  },

  /**
   * 点击tab切换
   */
  swichNav: function (e) {
    let status = e.target.dataset.current

    if (status === this.data.currentTab) return
    
    this.setData({
      currentTab: status,
      page: 1,
      couponList: [],
    })
    this.getCouponList(status, 1, this.data.channel)
  },

  // 返回上一页
  getAmount: function (e) {
    let status = e.currentTarget.dataset.status
    let amount = e.currentTarget.dataset.amount,
      number = e.currentTarget.dataset.number,
      pages = getCurrentPages(),
      prevPage = pages[pages.length - 2]

    if (status == 3 || status == 2) return false
    
    prevPage.setData({
      amount: amount,
      couponId: number
    })

    wx.navigateBack()
  },
  //返回所有
  getAllAmount: function (e){
    let status = e.currentTarget.dataset.status
    let number = e.currentTarget.dataset.number
    
    if (status == 3 || status == 2) return false

    wx.navigateTo({
      url: '/pages/categoryHall/categoryHall?coupon_no=' + number + '&isCoupon=true',
    })
  },

  useTap: function (e) {
    let currentCouponNo = e.currentTarget.dataset.id
    let couponList = this.data.couponList

    couponList.forEach((item) => {
      if (item.coupon_no == currentCouponNo) {
        if (item.show_flag) {
          item.show_flag = false
        } else {
          item.show_flag = true
        }
      } else {
        item.show_flag = false
      }

      this.setData({
        couponList: couponList
      })
    })
    this.setData({
      useflag: !this.data.useflag
    })
  }
})